﻿using System;
using System.Collections.Generic;
using BLL;
using BLL.RF.Package;
using Model;
using Model.RF.Package;

public partial class ProductPackage_SNUnion_Add : System.Web.UI.Page
{
    private PackageProduct pp;
    private ProductType p;

    public ProductPackage_SNUnion_Add()
    {
        pp = new PackageProduct();
        p = new ProductType();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Panel1.Visible = false;
        this.Panel2.Visible = false;
        this.TextBox6.Focus();
    }

    protected void TextBox6_TextChanged(object sender, EventArgs e)
    {
        ProductTypeInfo pi = p.GetProductTypebyName(this.TextBox6.Text.Trim());
        if (string.IsNullOrEmpty(pi.ProductTypeID))
        {
            string str = "没有检索到此产品类型，请先登记此产品类型信息！";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str + "');</script>");
            return;
        }
        DisplayforProductType(pi);

        this.TextBox1.Text = "";
        this.TextBox7.Text = "";
        this.TextBox2.Text = "";
        this.TextBox5.Text = "";
        this.TextBox1.Focus();
    }

    /// <summary>
    /// 在关联数据提交之前，有两次回传刷新，刷新后须用些方法使界面、界面数据与刷新前保持一致
    /// </summary>
    /// <param name="pid"></param>
    protected void DisplayforProductType(ProductTypeInfo pi)
    {
        switch (pi.HouseAmount)
        {
            case 2:
                this.Panel1.Visible = true;
                break;
            default:
                this.Panel1.Visible = false;
                break;
        }
        switch (pi.CustomerName)
        {
            case "阿朗(Alcatel-Lucent)":
            case "诺西(NSN)":
                this.Panel2.Visible = true;
                break;
            default:
                this.Panel2.Visible = false;
                break;
        }
    }

    /// <summary>
    /// 判断是否为二维码，并解析
    /// </summary>
    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
        string sn_2D = this.TextBox2.Text.Trim();
        if (sn_2D.Length >= 35)
        {
            this.TextBox5.Text = sn_2D.Substring(5, 13);
            this.TextBox2.Text = sn_2D.Substring(18, 12);
            this.Button2.Focus();
        }
        else
        {
            this.TextBox5.Focus();
        }
        ProductTypeInfo pi = p.GetProductTypebyName(this.TextBox6.Text.Trim());
        DisplayforProductType(pi);
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        string var_productSN = this.TextBox1.Text.Trim();
        string var_productSN2 = this.TextBox7.Text.Trim();
        List<string> prodcutSN_list = new List<string>();
        prodcutSN_list.Add(var_productSN);
        if (!string.IsNullOrEmpty(var_productSN2))
        { 
            prodcutSN_list.Add(var_productSN2); 
        }
        string var_packagePrdouctSN = this.TextBox2.Text.Trim();
        string var_SN3BK = this.TextBox5.Text.Trim();
        string var_productTypeName = this.TextBox6.Text.Trim();
        string userID = User.Identity.Name;
        string str;

        bool b = pp.ProductSNUnion(prodcutSN_list, var_packagePrdouctSN, var_SN3BK, var_productTypeName, userID, out str);
        if (b == true)
        {
            this.TextBox1.Text = "";
            this.TextBox2.Text = "";
            this.TextBox5.Text = "";       
        }
        ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str + "');</script>");
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        string var_productSN = this.TextBox3.Text.Trim();
        SNUnion su = new SNUnion();
        IList<SNUnionInfo> list = new List<SNUnionInfo>();
        SNUnionInfo sui = su.LoadEntity(var_productSN);
        if (!string.IsNullOrEmpty(sui.ProductSN))
        {
            list.Add(sui);
        }
        PackageProductInfo ppi = pp.GetPackageProductbyProductSN(var_productSN);
        if (!string.IsNullOrEmpty(ppi.ProductSN))
        {
            list.Add(new SNUnionInfo() { PackageProductSN = ppi.PackageProductSN, ProductSN = ppi.ProductSN, ProductTypeID = ppi.ProductTypeID, RecordTime = ppi.UnionTime, UserID = ppi.UserID });
        }
        this.GridView1.DataSource = list;
        this.GridView1.DataBind();
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        string var_packagePrdouctSN = this.TextBox4.Text.Trim();
        SNUnion su = new SNUnion();
        IList<SNUnionInfo> list = su.SNUnion_GetbyPackageProductSN(var_packagePrdouctSN);
        PackageProductInfo ppi = pp.GetPackageProductbyPackageProductSN(var_packagePrdouctSN);
        if (!string.IsNullOrEmpty(ppi.PackageProductSN))
        {
            list.Add(new SNUnionInfo() { PackageProductSN = ppi.PackageProductSN, ProductSN = ppi.ProductSN, ProductTypeID = ppi.ProductTypeID, RecordTime = ppi.UnionTime, UserID = ppi.UserID });
        }
        this.GridView2.DataSource = list;
        this.GridView2.DataBind();   
    }
}
